package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;
import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.UpdateOrderDeliveryTimeReq;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.UpdateOrderDeliveryTimeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderBizLogPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

public class Ts_SqUpdateOrderDeliveryTime {

	private static UpdateOrderDeliveryTimeReq updateOrderDeliveryTimeReq;
	private static UpdateOrderDeliveryTimeCall updateOrderDeliveryTimeCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新订单发货时间");
	}

	@Before
	public void caseUp() {
		updateOrderDeliveryTimeReq = new UpdateOrderDeliveryTimeReq();
		updateOrderDeliveryTimeCall = new UpdateOrderDeliveryTimeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateOrderDeliveryTimeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public static void verify(long orderId) {
		
		Logger.comment("验证order_detail_ext");

		Date deliveryTime = orderShardingWapper.selectOrderbyOrderId(orderId)
				.getDeliveryTime();

		DataUtil.verifyDateEquals(
				updateOrderDeliveryTimeReq.getNewDeliveryTime(), deliveryTime,
				"验证数据库deliveryTime");

		Logger.comment("验证order_biz_log");

		OrderBizLogPo orderBizLogPo = orderShardingWapper
				.selectOrderBizLogByOrderId(orderId);

		Logger.verifyEquals(orderId, orderBizLogPo.getOrderId(), "验证数据库OrderId");
		Logger.verifyEquals(updateOrderDeliveryTimeReq.getOperatorId(),
				orderBizLogPo.getOperator(), "验证数据库Operator");
		Logger.verifyEquals("ADD_SELLERMEMO", orderBizLogPo.getBizType(),
				"验证数据库BizType");
		Logger.verifyEquals(String.valueOf(orderId), orderBizLogPo.getBizNo(),
				"验证数据库BizNo");
		Logger.verifyEquals(String.format(
				"更新订单orderId:%s 发货时间:%s",
				orderId, updateOrderDeliveryTimeReq.getNewDeliveryTime()),
				orderBizLogPo.getContent(), "验证数据库Content");
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateOrderDeliveryTime_001() {
		Logger.start(true, "创建一个订单，店主更新订单发货时间成功");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, true);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, autotest_goodsOwnerId);
			
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			Long userId = resp.getUserId();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					resp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					resp.getPayableAmount().subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);

			SqTradingCallService.sellerAcceptOrder(orderId);
			SqTradingCallService.delivery(orderId);

			updateOrderDeliveryTimeReq.setOrderId(orderId);
			updateOrderDeliveryTimeReq.setSellerId(productBean.getGoodsOwnerId());
			updateOrderDeliveryTimeReq.setNewDeliveryTime(YMTDateUtil.getBeforeOrNextHour(YMTDateUtil.getDate(), 1));
			updateOrderDeliveryTimeReq.setOperatorId(1234L);

			updateOrderDeliveryTimeCall.setData(updateOrderDeliveryTimeReq);
			updateOrderDeliveryTimeCall.callService();

			verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateOrderDeliveryTime_002() {
		Logger.start(true, "创建一个订单，没有发货，货头更新订单发货时间失败");
		try {
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, autotest_goodsOwnerId);

			long orderId = resp.getOrderList().get(0).getOrderId();

			updateOrderDeliveryTimeReq.setOrderId(orderId);
			updateOrderDeliveryTimeReq.setSellerId(productBean.getGoodsOwnerId());
			updateOrderDeliveryTimeReq.setNewDeliveryTime(YMTDateUtil.getBeforeOrNextHour(YMTDateUtil.getDate(), 1));
			updateOrderDeliveryTimeReq.setOperatorId(1234L);

			updateOrderDeliveryTimeCall.setData(updateOrderDeliveryTimeReq);
			updateOrderDeliveryTimeCall.callService();
			
			Logger.verifyEquals(false, updateOrderDeliveryTimeCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(198,
					updateOrderDeliveryTimeCall.getCode(), "验证errorCode");
			Logger.verifyEquals(true, updateOrderDeliveryTimeCall.getMsg()
					.contains("订单没有发货"), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateOrderDeliveryTime_003() {
		Logger.start(true, "创建一个订单，中间商更新订单发货时间失败");
		try {

			updateOrderDeliveryTimeReq.setOrderId(1520043803L);
			updateOrderDeliveryTimeReq.setSellerId(501191469L);
			updateOrderDeliveryTimeReq.setNewDeliveryTime(YMTDateUtil.getBeforeOrNextHour(YMTDateUtil.getDate(), 1));
			updateOrderDeliveryTimeReq.setOperatorId(1234L);

			updateOrderDeliveryTimeCall.setData(updateOrderDeliveryTimeReq);
			updateOrderDeliveryTimeCall.callService();
			
			Logger.verifyEquals(false, updateOrderDeliveryTimeCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(198,
					updateOrderDeliveryTimeCall.getCode(), "验证errorCode");
			Logger.verifyEquals(true, updateOrderDeliveryTimeCall.getMsg()
					.contains("当前订单不允许更新发货时间"), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateOrderDeliveryTime_006() {
		Logger.start(true, "一个不存在的订单，店主更新发货时间失败");
		try {

			updateOrderDeliveryTimeReq.setOrderId(1L);
			updateOrderDeliveryTimeReq.setSellerId(autotest_goodsOwnerId);
			updateOrderDeliveryTimeReq.setNewDeliveryTime(YMTDateUtil.getBeforeOrNextHour(YMTDateUtil.getDate(), 1));
			updateOrderDeliveryTimeReq.setOperatorId(1234L);

			updateOrderDeliveryTimeCall.setData(updateOrderDeliveryTimeReq);
			updateOrderDeliveryTimeCall.callService();

			Logger.verifyEquals(false, updateOrderDeliveryTimeCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(198,
					updateOrderDeliveryTimeCall.getCode(), "验证errorCode");
			Logger.verifyEquals(true, updateOrderDeliveryTimeCall.getMsg()
					.contains("订单不存在"), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateOrderDeliveryTime_007() {
		Logger.start(true, "创建一个订单，店主更新发货时间，货头再次更新发货时间成功");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, autotest_goodsOwnerId);
			
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			Long userId = resp.getUserId();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					resp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					resp.getPayableAmount().subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);

			SqTradingCallService.sellerAcceptOrder(orderId);
			SqTradingCallService.delivery(orderId);

			// 第一次更新备注
			updateOrderDeliveryTimeReq.setOrderId(orderId);
			updateOrderDeliveryTimeReq.setSellerId(productBean.getSellerId());
			updateOrderDeliveryTimeReq.setNewDeliveryTime(YMTDateUtil.getBeforeOrNextHour(YMTDateUtil.getDate(), 1));
			updateOrderDeliveryTimeReq.setOperatorId(4321L);

			updateOrderDeliveryTimeCall.setData(updateOrderDeliveryTimeReq);
			updateOrderDeliveryTimeCall.callService();

			// 第二次更新备注
			updateOrderDeliveryTimeReq.setOrderId(orderId);
			updateOrderDeliveryTimeReq.setSellerId(autotest_goodsOwnerId);
			updateOrderDeliveryTimeReq.setNewDeliveryTime(YMTDateUtil.getBeforeOrNextHour(YMTDateUtil.getDate(), 1));
			updateOrderDeliveryTimeReq.setOperatorId(1234L);

			updateOrderDeliveryTimeCall.setData(updateOrderDeliveryTimeReq);
			updateOrderDeliveryTimeCall.callService();

			verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateOrderDeliveryTime_008() {
		Logger.start(true, "创建一个订单，货头更新发货时间成功");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, autotest_goodsOwnerId);
			
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			Long userId = resp.getUserId();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					resp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					resp.getPayableAmount().subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);

			SqTradingCallService.sellerAcceptOrder(orderId);
			SqTradingCallService.delivery(orderId);

			updateOrderDeliveryTimeReq.setOrderId(orderId);
			updateOrderDeliveryTimeReq.setSellerId(autotest_goodsOwnerId);
			updateOrderDeliveryTimeReq.setNewDeliveryTime(YMTDateUtil.getBeforeOrNextHour(YMTDateUtil.getDate(), 1));
			updateOrderDeliveryTimeReq.setOperatorId(1234L);

			updateOrderDeliveryTimeCall.setData(updateOrderDeliveryTimeReq);
			updateOrderDeliveryTimeCall.callService();

			verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
